package com.sheng.project.security.webmvc.property;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.*;

/**
 * @author by ls
 * @date 2023/9/8
 */
@ConfigurationProperties(prefix = "application.security")
public class SecurityApplicationProperties {

    private final Map<String, Matcher> matchers = new HashMap<>();

    private final JWT jwt = new JWT();

    private final List<String> modes = new LinkedList<>();

    private boolean customLoginPage = false;

    private String loginPage;

    private String loginProcessingUrl;

    public SecurityApplicationProperties() {
        Matcher def = new Matcher();
        def.permit.addAll(Arrays.asList("/*.css", "/*.html", "/*.ico", "/*.js", "/*.txt"));
        def.permit.addAll(Arrays.asList("/actuator", "/actuator/**", "/error", "/swagger-resources/**", "/swagger-ui/**", "/v2/api-docs/**", "/webjars/**"));
        def.permit.addAll(Arrays.asList("/ip/test"));
        this.matchers.put("_def", def);
        this.modes.add("resource");
    }

    public String getLoginProcessingUrl() {
        return this.loginProcessingUrl;
    }

    public String getLoginPage() {
        return this.loginPage;
    }

    public boolean isCustomLoginPage() {
        return customLoginPage;
    }

    public Map<String, Matcher> getMatchers() {
        return matchers;
    }

    public List<String> getModes() {
        return modes;
    }

    public JWT getJwt() {
        return jwt;
    }

    public static class Matcher {

        private final List<String> permit = new LinkedList<>();

        private final List<String> authenticate = new LinkedList<>();

        public List<String> getPermit() {
            return permit;
        }

        public List<String> getAuthenticate() {
            return authenticate;
        }
    }

    @Data
    public static class JWT {

        private String iss;

        private String sub;
    }
}
